// pages/zeroList/index.js
var common = require("../../public.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
      filterList:[
          {name:'综合',select:true,index:0,value:''},{name:'附近',select:false,index:1,value:''},{name:'销量',select:false,index:2,value:''},{name:'时间',select:false,index:3,value:''}],
    zeroData: [],
    pageNum: 1,
    distanceSort: 1,
    saleSort: 1,
    timeSort: 1,
    xpoint: '',
    ypoint: '',

    isLoadingBusy: false,
    noMoreData: false,
    refreshing: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getZeroList(true)
  },
  onReachBottom: function () {
    this.getZeroList(false)
  },

  onPullDownRefresh: function () {
    this.getZeroList(true)
  },
  getZeroList(refresh = false) {
    const that = this
    if (refresh) {
      that.setData({
        noMoreData: false
      })
    }
    if (that.data.noMoreData || that.data.isLoadingBusy) {
      return
    }
    if (refresh) {
      that.setData({
        refreshing: true
      })
    } else {
      this.setData({
        isLoadingBusy: true
      })
    }
    const pageNum = refresh ? 1 : this.data.pageNum + 1
    let tempJson = {
        pageNum,
    //   distanceSort: that.data.distanceSort,// 距离排序 1 从近到远 2 从远到近
    //   saleSort: that.data.saleSort,// 销量排序 1从少到多 2从多到少
    //   timeSort: that.data.timeSort,// 结束时间排序 1结束早的排前面 2结束晚的排前面
    distanceSort:that.data.filterList[0].value,// 距离排序 1 从近到远 2 从远到近
    saleSort:that.data.filterList[1].value,// 销量排序 1从少到多 2从多到少
    timeSort: that.data.filterList[2].value,// 结束时间排序 1结束早的排前面 2结束晚的排前面


      xpoint: that.data.xpoint,
      ypoint: that.data.ypoint,
    }

    common.post('/findZeroBuyGoodsList', tempJson, function (res) {

      if (res.status === 200) {
        that.data.pageNum = pageNum
        if (res.data.result_list.length) {
          const list = refresh ? res.data.result_list : [...that.data.zeroData, ...res.data.result_list]
          that.setData({
            zeroData: list
          })
        } else {
          that.setData({
            noMoreData: true
          })
        }
      }
    }, function () {
        console.log('这里执行了=----');
        that.setData({
          isLoadingBusy: false,
          refreshing: false
        })
        if (refresh) {
          //停止下拉刷新
          wx.stopPullDownRefresh()
        }
      })
  },
  filterSelect(event){
    console.log(event)
    const { index } = event.currentTarget.dataset;
    // let {
    //   item
    // } = e.currentTarget.dataset
    console.log('index--',index);

    for(var i = 0;i < this.data.filterList.length;i++){
        var item1 = this.data.filterList[i];
        // console.log('item--',item);
        item1.select = false;
        item1.vaule = '';
    }
   
    // var jsonItem = JSON.parse(item);
    console.log('index---',index);
    this.data.filterList[index].select = true;
    this.data.filterList[index].value = 2;
    console.log('filterList--',this.data.filterList);
    this.setData({
        filterList:this.data.filterList
    })
    this.getZeroList(true);
  },
  toZeroDetail(e) {
    console.log(e)
    let {
      item
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/packageC/pages/Zero/Des/index?id=' + item.id + "&name=" + item.goodsTitle,
    })
  }
})